﻿#include "uimsetup.h"
#include "ui_uimsetup.h"
#include "tool.h"

UIMsetup::UIMsetup(QWidget *parent , fsConfig *conf, UIM *uim, const QString msg) :
    QWidget(parent),
    ui(new Ui::UIMsetup)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    setWindowFlags(windowFlags()&~Qt::WindowMinimizeButtonHint);

    title = msg;
    //retInfo(title);

    if(conf != nullptr){
        fsConf = conf;
        initUI();
    }
    fsUim = uim;
    if(fsUim != nullptr && fsUim->isOpen()){
        ui->pushButtonFindGateway->setEnabled(false);
        ui->comboBoxGatewayList->addItems(fsUim->getList());
    }

    isUimSave = false;
}

UIMsetup::~UIMsetup()
{
    delete ui;
}

void UIMsetup::setWin(int w,int h){
    resize(w,h);
    setFixedSize(this->width(),this->height());
}

void UIMsetup::closeEvent(QCloseEvent *event)
{
    if((isUimSave)&& QMessageBox::question(this,
                                            QString("保存"),
                                            QString("配置是否保存退出?，请确认"),
                                            QMessageBox::Yes,
                                            QMessageBox::No) == QMessageBox::Yes){
        on_pushButtonSave_clicked();
    }
    fsUim->canFocusOff();
    fsUim->canRightOff();
    event->accept();
}

void UIMsetup::initUI(void)
{
    uimVar = &(fsConf->uimVar);
    setWindowTitle(QString("%0").arg(title));

    QString sheet;
    if(is4KScreen()){
        sheet = getComboBoxSheet(fsConf->designVar.ComboBoxItemHeight4K);
    }else{
        sheet = getComboBoxSheet(fsConf->designVar.ComboBoxItemHeight);
    }
    //
    ui->checkBoxUSB->setChecked(uimVar->UIMUsbPort);
    ui->checkBoxCOM->setChecked(uimVar->UIMComPort);
    //调试模式
    ui->checkBoxUimDebug->setChecked(uimVar->Debug);
    //上下
    ui->spinBoxNodeIdFocus->setValue(uimVar->UIMNodeIdFocus);
    ui->spinBoxCanRateFocus->setValue(uimVar->UIMCanRateFocus);
    ui->spinBoxCanRateFocusLocate->setValue(uimVar->UIMCanRateFocusLocate);
    ui->spinBoxCanStepFocus->setValue(uimVar->UIMCanStepFocus);
    ui->spinBoxCanStepFocusLocate->setValue(uimVar->UIMCanStepFocusLocate);
    ui->checkBoxCanFocusOrg->setChecked(uimVar->UIMCanFocusOrg);
    ui->spinBoxCanFocusFar->setValue(uimVar->UIMCanFocusFar);
    ui->spinBoxCanFocusLimit1->setValue(uimVar->UIMCanFocusLimit1);
    ui->spinBoxCanFocusLimit2->setValue(uimVar->UIMCanFocusLimit2);
    ui->checkBoxUimFocusMode->setChecked(uimVar->UIMCanFocusMode);
    //卷片扫描
    ui->spinBoxCanRatePhoto->setValue(uimVar->UIMCanRatePhoto);
    //右电机
    ui->spinBoxNodeIdRight->setValue(uimVar->UIMNodeIdRight);
    ui->spinBoxCanRateRight->setValue(uimVar->UIMCanRateRight);
    ui->spinBoxCanStepRight->setValue(uimVar->UIMCanStepRight);
    ui->spinBoxCanRateTakeupRight->setValue(uimVar->UIMCanRateTakeupRight);
    ui->checkBoxCanEncodeStop->setChecked(uimVar->UIMCanEncodeStop);
    ui->comboBoxGatewayList->clear();
}

// 搜索
void UIMsetup::on_pushButtonFindGateway_clicked()
{
    ui->comboBoxGatewayList->clear();
    if(fsUim->canOpenGateWay()){
        ui->comboBoxGatewayList->addItems(fsUim->getList());
        emit uimFlush();
    }
}

void UIMsetup::on_pushButtonSave_clicked()
{
    uimVar->UIMUsbPort = ui->checkBoxUSB->isChecked();
    uimVar->UIMComPort = ui->checkBoxCOM->isChecked();
    //调试模式
    uimVar->Debug = ui->checkBoxUimDebug->isChecked();
    //上下
    uimVar->UIMNodeIdFocus = ui->spinBoxNodeIdFocus->value();
    uimVar->UIMCanRateFocus = ui->spinBoxCanRateFocus->value();
    uimVar->UIMCanRateFocusLocate = ui->spinBoxCanRateFocusLocate->value();
    uimVar->UIMCanStepFocus = ui->spinBoxCanStepFocus->value();
    uimVar->UIMCanStepFocusLocate = ui->spinBoxCanStepFocusLocate->value();
    uimVar->UIMCanFocusOrg = ui->checkBoxCanFocusOrg->isChecked();
    uimVar->UIMCanFocusFar = ui->spinBoxCanFocusFar->value();
    uimVar->UIMCanFocusLimit1 = ui->spinBoxCanFocusLimit1->value();
    uimVar->UIMCanFocusLimit2 = ui->spinBoxCanFocusLimit2->value();
    uimVar->UIMCanFocusMode = ui->checkBoxUimFocusMode->isChecked();
    //卷片扫描
    uimVar->UIMCanRatePhoto = ui->spinBoxCanRatePhoto->value();
    //右电机
    uimVar->UIMNodeIdRight = ui->spinBoxNodeIdRight->value();
    uimVar->UIMCanRateRight = ui->spinBoxCanRateRight->value();
    uimVar->UIMCanStepRight = ui->spinBoxCanStepRight->value();
    uimVar->UIMCanRateTakeupRight = ui->spinBoxCanRateTakeupRight->value();
    uimVar->UIMCanEncodeStop = ui->checkBoxCanEncodeStop->isChecked();
    //保存
    if(isUimSave){
        fsConf->saveConfigUimConf();
        emit uimSave();
    }
    isUimSave = false;
}

// USB网关
void UIMsetup::on_checkBoxUSB_toggled(bool checked)
{
    Q_UNUSED(checked);
    isUimSave = true;
}

// 串口网关
void UIMsetup::on_checkBoxCOM_toggled(bool checked)
{
    Q_UNUSED(checked);
    isUimSave = true;
}

//调试模式
void UIMsetup::on_checkBoxUimDebug_toggled(bool checked)
{
    if(ui->checkBoxUimDebug->isChecked() == !checked){
        ui->checkBoxUimDebug->setChecked(!checked);
    }
    isUimSave = true;
}

//调焦
void UIMsetup::on_spinBoxNodeIdFocus_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}
void UIMsetup::on_spinBoxCanRateFocus_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}
void UIMsetup::on_spinBoxCanRateFocusLocate_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}
void UIMsetup::on_spinBoxCanStepFocus_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}
void UIMsetup::on_spinBoxCanStepFocusLocate_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}
void UIMsetup::on_checkBoxCanFocusOrg_toggled(bool checked)
{
    Q_UNUSED(checked);
    isUimSave = true;
}
void UIMsetup::on_spinBoxCanFocusFar_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}
void UIMsetup::on_spinBoxCanFocusLimit1_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}
void UIMsetup::on_spinBoxCanFocusLimit2_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}
void UIMsetup::on_checkBoxUimFocusMode_toggled(bool checked)
{
    Q_UNUSED(checked);
    isUimSave = true;
}

//右收片
void UIMsetup::on_spinBoxNodeIdRight_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}

void UIMsetup::on_spinBoxCanRateRight_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}

void UIMsetup::on_spinBoxCanStepRight_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}

void UIMsetup::on_spinBoxCanRateTakeupRight_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}

void UIMsetup::on_spinBoxCanRatePhoto_valueChanged(int arg1)
{
    Q_UNUSED(arg1);
    isUimSave = true;
}

void UIMsetup::on_checkBoxCanEncodeStop_toggled(bool checked)
{
    Q_UNUSED(checked);
    isUimSave = true;
}

